package com.github.postsanf.yinian.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.ChoosePicActivity;
import com.github.postsanf.yinian.utils.DisplayUtils;

import java.util.List;

/**
 * Created by Administrator on 2016/9/11.
 */
public class LoMoAdapter extends BaseAdapter{

    private  List<Bitmap> bitmapList;
    private Context mContext;
    private LayoutInflater mInflater;

    public LoMoAdapter(Context mContext,List<Bitmap> bitmapList){
        mInflater = LayoutInflater.from(mContext);
        this.bitmapList=bitmapList;
        this.mContext=mContext;
    }

    @Override
    public int getCount() {
        return bitmapList.size();
    }

    @Override
    public Object getItem(int position) {
        return bitmapList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=null;
        if (convertView==null){
            viewHolder = new ViewHolder();
            convertView=mInflater.inflate(R.layout.lomo_item,parent,false);
            viewHolder.iv_lomo_bitmap=((ImageView) convertView.findViewById(R.id.iv_lomo_bitmap));
            viewHolder.ll_bg= ((ImageView) convertView.findViewById(R.id.ll_bg));
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }

        int screenWidth_bg = DisplayUtils.getScreenWidthPixels((Activity) mContext);
        int horizontalSpacing_bg = DisplayUtils.dp2px(mContext, 15+15+24);
        int itemWidth_bg = (screenWidth_bg -  horizontalSpacing_bg) / 2;
        RelativeLayout.LayoutParams params_bg = new RelativeLayout.LayoutParams(itemWidth_bg, (int)(itemWidth_bg*1.2));
        viewHolder.ll_bg.setLayoutParams(params_bg);

        int screenWidth = DisplayUtils.getScreenWidthPixels((Activity) mContext);
        int horizontalSpacing = DisplayUtils.dp2px(mContext, 9);
        int itemWidth = (itemWidth_bg);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(itemWidth, itemWidth);
        viewHolder.iv_lomo_bitmap.setLayoutParams(params);

        viewHolder.iv_lomo_bitmap.setImageBitmap(bitmapList.get(position));

        viewHolder.iv_lomo_bitmap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mContext.startActivity(new Intent(mContext,ChoosePicActivity.class).putExtra("position",position));
            }
        });
        return convertView;
    }

    public static class ViewHolder {
        public ImageView iv_lomo_bitmap;
        public ImageView ll_bg;
    }
}
